import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class Daily_ResourceDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;


    public static async insert_daily_resource() {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select sum(gems) as gems,sum(coins) as coins,sum(meilis) as meilis, sum(income) as income from t_users";
        let sqlParams = [];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("get_main_statistics_info=", results)
        let resultObj: object = results[0];
        sql = "replace into t_daily_resource(date, gems, coins, meilis, income) values(?,?,?,?,?)"
        sqlParams = [DateUtil.formatDateStr3(DateUtil.now()), resultObj["gems"], resultObj["coins"], resultObj["meilis"], resultObj["income"]]
        await client.queryPromise(sql, sqlParams);
    }

    public static async get_daily_resource() {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select * from t_daily_resource";
        let sqlParams = [];
        let results = await client.queryPromise(sql, sqlParams);
        sql = "select sum(gems) as gems,sum(coins) as coins,sum(meilis) as meilis, sum(income) as income  from t_users";
        let todayResult = await client.queryPromise(sql, sqlParams);
        Logger.log("get_main_statistics_info=", results)
        Logger.log("get_main_statistics_info=", todayResult)
        Logger.log("get_daily_resource=", results)
        let tempObj:object = todayResult[0]
        tempObj["date"] = DateUtil.now()
        results.push(tempObj)
        return results
    }

    public static async clear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_daily_resource where date < ?"
        let sqlParams = [DateUtil.formatDateStr3(delete_time)];
        await client.queryPromise(sql, sqlParams);
    }

}